
package com.mflex.WHSmartWhouseAd.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.mflex.WHSmartWhouseAd.R;
import com.mflex.WHSmartWhouseAd.adapter.WMSOutStockTaskDetailAdapter;
import com.mflex.WHSmartWhouseAd.adapter.WMSOutStockTaskDetailSmartAdapter;
import com.mflex.WHSmartWhouseAd.model.DelisPlateInfoModel;
import com.mflex.WHSmartWhouseAd.model.DelisScanDataReturnModel;
import com.mflex.WHSmartWhouseAd.model.DelisScanSmartDataReturnModel;
import com.mflex.WHSmartWhouseAd.model.DelisVehicleInfoModel;
import com.mflex.WHSmartWhouseAd.model.DoTransTaskInfo;
import com.mflex.WHSmartWhouseAd.model.MaterialDetailForDelisModel;
import com.mflex.WHSmartWhouseAd.model.RequestModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailInfoForDelisModel;
import com.mflex.WHSmartWhouseAd.model.TaskDetailSubimtForDelistModel;
import com.mflex.WHSmartWhouseAd.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xhttp2.reflect.TypeToken;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class WMSOutStockTaskDetailSmartActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    EditText txtTaskNo;
    EditText txtJDEPN;
    EditText txtTransType;

    EditText txtMaterialType;
    EditText txtUnremovedQty;
    EditText txtRemovedQty;

    ListView txtList;
    MiniLoadingDialog mMiniLoadingDialog;

    EditText txtVehicleNO;
    EditText txtPanelNo;

    Button buttonClear;
    Button buttonSubmit;
    String userName;
    String StorageType;

    String TaskDetailID;

    private double scanQty;
    DelisScanSmartDataReturnModel DelisScanSmartDataReturnModel;
    TaskDetailInfoForDelisModel taskDetailInfoForDelis;
    List<MaterialDetailForDelisModel> MaterialDetailForDelisModel;

    List<DelisVehicleInfoModel> DelisVehicleInfoList=new ArrayList<>();
    List<DelisPlateInfoModel> DelisPlateInfoList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_wms_outstock_task_detail_smart);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "加载...");

        txtTaskNo= findViewById(R.id.stvTaskNO);
        txtJDEPN= findViewById(R.id.stvJDEPN);
        txtTransType= findViewById(R.id.stvTransType);

        txtMaterialType= findViewById(R.id.stvMaterialType);
        txtUnremovedQty= findViewById(R.id.stvUnremovedQty);
        txtRemovedQty= findViewById(R.id.stvRemovedQty);

        txtRemovedQty= findViewById(R.id.stvRemovedQty);

        txtList= findViewById(R.id.lvList);
        buttonSubmit= findViewById(R.id.button_save);
        buttonClear= findViewById(R.id.button_clear);
        txtPanelNo= findViewById(R.id.stvPanelNo);

        txtVehicleNO=findViewById(R.id.stvVehicelNo);

        Intent intent = getIntent();
        userName = intent.getStringExtra("OPT");
        txtTaskNo.setText(intent.getStringExtra("TaskNo"));
        StorageType=intent.getStringExtra("StorageType");

        txtTaskNo.setText(intent.getStringExtra("TaskNo"));
        txtJDEPN.setText(intent.getStringExtra("JDEPN"));
        txtTransType.setText(intent.getStringExtra("TaskType"));
        txtMaterialType.setText(intent.getStringExtra("MaterialClass"));
        txtUnremovedQty.setText(intent.getStringExtra("Qty"));

        TaskDetailID=intent.getStringExtra("TaskDetailID");


        Type listType = new TypeToken<List<MaterialDetailForDelisModel>>() {
        }.getType();
        MaterialDetailForDelisModel = (List<MaterialDetailForDelisModel>) JsonUtil.fromJson(intent.getStringExtra("list"), listType);
        WMSOutStockTaskDetailSmartAdapter WMSOutStockTaskDetailAdapter = new WMSOutStockTaskDetailSmartAdapter(WMSOutStockTaskDetailSmartActivity.this, MaterialDetailForDelisModel);
        txtList.setAdapter(WMSOutStockTaskDetailAdapter);
        txtVehicleNO.requestFocus();
        txtVehicleNO.setText("");


        buttonSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

//                mMiniLoadingDialog.show();
                TaskDetailSubimtForDelistModel model=new TaskDetailSubimtForDelistModel(TaskDetailID,txtMaterialType.getText().toString(),DelisVehicleInfoList,userName);
                RequestModel requestModel = new RequestModel();
                requestModel.setJson(JsonUtil.toJson(model));
                String json = JsonUtil.toJson(requestModel);
                System.out.println(json);
                XHttp.post("api/WHSmartWhousePDA/STaskSubmitForDelist")
                        .upJson(JsonUtil.toJson(requestModel))
                        .syncRequest(false)
                        .onMainThread(true)
                        .timeOut(150000)
                        .execute(new SimpleCallBack<Boolean>() {
                            @Override
                            public void onSuccess(Boolean response) {
                                mMiniLoadingDialog.dismiss();
                                XToastUtils.success("提交成功",3000);
                                Intent intent=new Intent(WMSOutStockTaskDetailSmartActivity.this,WMSOutStockTaskActivity.class);
                                startActivity(intent);
                            }

                            @Override
                            public void onError(ApiException e) {
                                mMiniLoadingDialog.dismiss();
                                MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskDetailSmartActivity.this, R.raw.error_voice);mp.start();
                                new MaterialDialog.Builder(WMSOutStockTaskDetailSmartActivity.this)
                                        .iconRes(R.drawable.icon_warning)
                                        .title(R.string.tip_warning)
                                        .content(e.getMessage())
                                        .positiveText(R.string.lab_submit)
                                        .show();
                            }
                        });

            }
        });

        txtVehicleNO.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
//                mMiniLoadingDialog.show();
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    if (txtVehicleNO.getText().toString() == "") {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskDetailSmartActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描载具码", 1500);
                        txtVehicleNO.requestFocus();
                        txtVehicleNO.setText("");
                        return false;
                    }

                    XHttp.get("api/WHSmartWhousePDA/DelisScanSVehicle")
                            .params("TaskNo", txtTaskNo.getText().toString())
                            .params("VehicleNumber", txtVehicleNO.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<DelisScanSmartDataReturnModel>() {
                                @Override
                                public void onSuccess(DelisScanSmartDataReturnModel response) {

                                    mMiniLoadingDialog.dismiss();
                                    DelisScanSmartDataReturnModel=response;
                                    scanQty = scanQty + Double.parseDouble(response.getQty());
                                    txtRemovedQty.setText(String.valueOf(scanQty));

                                    DelisPlateInfoModel DelisPlateInfoModel=new DelisPlateInfoModel(response.getPlateNumber().toString(),response.getQty().toString());
                                    DelisPlateInfoList.add(DelisPlateInfoModel);

                                    for (MaterialDetailForDelisModel detail : MaterialDetailForDelisModel) {
                                        if (StringUtils.equals(response.getVehicleNumber(), detail.getVehicleNumber())) {
                                            if(StringUtils.equals("2", detail.getflg()))
                                            {
                                                mMiniLoadingDialog.dismiss();
                                                MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskDetailSmartActivity.this, R.raw.error_voice);mp.start();
                                                XToastUtils.success("该载具已扫描", 1500);
                                                txtVehicleNO.requestFocus();
                                                txtVehicleNO.setText("");
                                                break;
                                            }
                                            detail.setflg("2");
                                            break;
                                        }

                                    }

                                    if(DelisVehicleInfoList==null)
                                    {
                                        DelisVehicleInfoModel Mode=new DelisVehicleInfoModel(response.getVehicleNumber().toString(),response.getQty().toString(),DelisPlateInfoList);
                                        DelisVehicleInfoList.add(Mode);
                                    }
                                    else {
                                        boolean flg=false;
                                        for(DelisVehicleInfoModel vehInfo:DelisVehicleInfoList)
                                        {

                                            if(StringUtils.equals(vehInfo.getVehicleNumber(), response.getVehicleNumber().toString()))
                                            {
                                                double totalqty=Double.parseDouble(vehInfo.getQty())+Double.parseDouble(response.getQty());
                                                vehInfo.setQty(String.valueOf(totalqty));
                                                flg=true;
                                                break;
                                            }
                                        }
                                        if(!flg)
                                        {
                                            DelisVehicleInfoModel Mode=new DelisVehicleInfoModel(response.getVehicleNumber().toString(),response.getQty().toString(),DelisPlateInfoList);
                                            DelisVehicleInfoList.add(Mode);
                                        }
                                    }

                                    WMSOutStockTaskDetailSmartAdapter WMSOutStockTaskDetailAdapter = new WMSOutStockTaskDetailSmartAdapter(WMSOutStockTaskDetailSmartActivity.this, MaterialDetailForDelisModel);
                                    txtList.setAdapter(WMSOutStockTaskDetailAdapter);
                                    txtVehicleNO.setText("");
                                    txtVehicleNO.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    txtVehicleNO.setText("");
                                    txtVehicleNO.requestFocus();
                                    MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskDetailSmartActivity.this, R.raw.error_voice);mp.start();
                                    new MaterialDialog.Builder(WMSOutStockTaskDetailSmartActivity.this)
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });

        txtPanelNo.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent event) {
//                mMiniLoadingDialog.show();
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {

                    if (txtPanelNo.getText().toString() == "") {
                        mMiniLoadingDialog.dismiss();
                        MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskDetailSmartActivity.this, R.raw.error_voice);mp.start();
                        XToastUtils.success("请先扫描标签", 1500);
                        txtPanelNo.requestFocus();
                        return false; 
                    }

                    XHttp.get("api/WHSmartWhousePDA/DelisScanSBarcode")
                            .params("TaskNo", txtTaskNo.getText().toString())
                            .params("MaterialType", txtMaterialType.getText().toString())
                            .params("Barcode", txtPanelNo.getText().toString())
                            .syncRequest(false)
                            .onMainThread(true)
                            .timeOut(120000)
                            .execute(new SimpleCallBack<DelisScanSmartDataReturnModel>() {
                                @Override
                                public void onSuccess(DelisScanSmartDataReturnModel response) {

                                    mMiniLoadingDialog.dismiss();
                                    DelisScanSmartDataReturnModel=response;
                                    scanQty = scanQty + Double.parseDouble(response.getQty());
                                    txtRemovedQty.setText(String.valueOf(scanQty));

                                    DelisPlateInfoModel DelisPlateInfoModel=new DelisPlateInfoModel(response.getPlateNumber().toString(),response.getQty().toString());
                                    DelisPlateInfoList.add(DelisPlateInfoModel);

                                    for (MaterialDetailForDelisModel detail : MaterialDetailForDelisModel) {

                                        if (StringUtils.equals(response.getVehicleNumber(), detail.getVehicleNumber())) {
                                             if(StringUtils.equals("", detail.getScanQty()))
                                             {
                                                 detail.setScanQty(String.valueOf(Double.parseDouble(response.getQty())));

                                             }
                                             else
                                             {
                                                 detail.setScanQty(String.valueOf(Double.parseDouble(detail.getScanQty())+Double.parseDouble(response.getQty())));

                                             }
                                            detail.setflg("1");//未下完黄色
                                            int qty=(int)Math.round(Double.parseDouble(txtRemovedQty.getText().toString()));
                                            if(StringUtils.equals(String.valueOf(qty), txtUnremovedQty.getText().toString()))
                                            {
                                                detail.setflg("2");//都下完是绿色
                                            }

                                            break;
                                        }

                                    }

                                    if(DelisVehicleInfoList==null)
                                    {
                                        DelisVehicleInfoModel Mode=new DelisVehicleInfoModel(response.getVehicleNumber().toString(),response.getQty().toString(),DelisPlateInfoList);
                                        DelisVehicleInfoList.add(Mode);
                                    }
                                    else {
                                        boolean flg=false;
                                        for(DelisVehicleInfoModel vehInfo:DelisVehicleInfoList)
                                        {

                                            if(StringUtils.equals(vehInfo.getVehicleNumber(), response.getVehicleNumber().toString()))
                                            {
                                                double totalqty=Double.parseDouble(vehInfo.getQty())+Double.parseDouble(response.getQty());
                                                vehInfo.setQty(String.valueOf(totalqty));
                                                flg=true;
                                                break;
                                            }
                                        }
                                        if(!flg)
                                        {
                                            DelisVehicleInfoModel Mode=new DelisVehicleInfoModel(response.getVehicleNumber().toString(),response.getQty().toString(),DelisPlateInfoList);
                                            DelisVehicleInfoList.add(Mode);
                                        }
                                    }

                                    WMSOutStockTaskDetailSmartAdapter WMSOutStockTaskDetailAdapter = new WMSOutStockTaskDetailSmartAdapter(WMSOutStockTaskDetailSmartActivity.this, MaterialDetailForDelisModel);
                                    txtList.setAdapter(WMSOutStockTaskDetailAdapter);
                                    txtPanelNo.setText("");
                                    txtPanelNo.requestFocus();
                                }

                                @Override
                                public void onError(ApiException e) {
                                    mMiniLoadingDialog.dismiss();
                                    MediaPlayer mp = MediaPlayer.create(WMSOutStockTaskDetailSmartActivity.this, R.raw.error_voice);mp.start();
                                    new MaterialDialog.Builder(view.getContext())
                                            .iconRes(R.drawable.icon_warning)
                                            .title(R.string.tip_warning)
                                            .content(e.getMessage())
                                            .positiveText(R.string.lab_submit)
                                            .show();
                                }
                            });
                }
                return false;
            }
        });



    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }
}



